<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
/*use Illuminate\Foundation\Auth\ThrottlesLogins;*/
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
//控制器创建: php artisan make:controller Admin/LoginController

class LoginController extends Controller
{
  /*  use ThrottlesLogins;*/
    //$username默认email；如果管理员使用laravel默认的user表，可以不做指定
    public $username = 'username';
    //$maxAttempts默认5
  /*  public $maxAttempts = 2;*/

    public function __construct()
    {
        //配置信息
        $config = configs();
        return view()->share('config',$config);
    }

    //登录页面
    //管理员登录(模型中需要引入User) 辅助函数；auth;配置Middleware\Authenticate;设置web.php路由
    public function index(Request $request)
    {
        if($request->isMethod('post')){
           /* if ($this->hasTooManyLoginAttempts($request)){
                return response()->json(['code' => 0, 'msg' => '错误登录次数过多，请稍后重试！']);
            }*/
            $configs= configs('verify');
            if($configs['code'] == 'open'){
                //表单验证
                $validator = Validator::make($request->all(),[
                    'code'=>'required|captcha',
                ],[
                    'code.captcha'=>'验证码错误',
                ]);
                if ($validator->fails()) {
                    return response()->json([
                        'code' => 0,
                        'msg'  => $validator->errors()->first(),
                    ], 200);
                }
            }

            //在config/auth.php 配置相关信息，在Model/Admin.php(use \Illuminate\Foundation\Auth\User;并继承User)
            $result = Auth::guard('admin')->attempt(['username'=>$request->username,'password'=>$request->password]);
            // $result = Auth::attempt(['username'=>$request->username,'password'=>$request->password]);
            if($result){
                if(Auth::user()->is_open == 0){
                    Auth::logout();
                    return response()->json(['code' => 0, 'msg' => '账户状态异常!']);
                }
                session(['loginip_admin'=>Auth::user()->loginip]);
                session(['ipadress_admin'=>Auth::user()->ipadress]);
                session(['logintime_admin'=>Auth::user()->logintime]);
                session()->save();

                $arr['loginip'] = $request->ip();
                $location = new \App\Library\location\IpLocation();
                $ips = $location->getlocation($arr['loginip']);
                $arr['ipadress'] = $ips['country'];
                $arr['logintime'] = time();
                $arr['loginnum'] = (Auth::user()->loginnum)+1;
                //session()->put('adminid_admin',Auth::user()->id);
                DB::table("admins")->where('id',(Auth::user()->id))->update($arr);
                return response()->json(['code' => 1, 'msg' => '登入成功!']);
            }else{
                //$this->incrementLoginAttempts($request);
                return response()->json(['code' => 0, 'msg' => '用户名或密码错误!']);
            }
        }else{
            //表必须自增键必须设置成id
            if(Auth::check()){
                return redirect('admin/index/index');
            }
            return view('admin/login/index');
        }

    }

    //退出登录
    public function loginout(){
        Auth::logout();
        $url =  url('adminlogin');
        echo "<script>parent.location.href ='".$url."'</script>";

        //return redirect('adminlogin');
    }


    //property_exists — 检查对象或类是否具有该属性
    protected function username()
    {
        return property_exists($this, 'username') ? $this->username : 'email';
    }

    protected function throttleKey(Request $request)
    {
        return Str::lower('admin.'.$request->input($this->username())).'|'.$request->ip();
    }
}
